﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SmsUI.DataBase;
using SMSCore;
using SmsUI.N77;

namespace SmsUI
{
    public partial class SetModifyForm : DevExpress.XtraEditors.XtraForm
    {
        smsdbDataSet.tb_SetDataTable tb = null;
        private int[] selectRow;
        public SetModifyForm(int[] selectRow)
        {
            InitializeComponent();

            this.selectRow = selectRow;
            this.Load += new EventHandler(SetModifyForm_Load);

        }

        void SetModifyForm_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void InitData()
        {
            foreach (string s in ComHelper.Instance.GetAvailComPorts())
            {
                this.comPortNum.Properties.Items.Add(s);
            }
            comPortNum.SelectedValueChanged += new EventHandler(comPortNum_SelectedValueChanged);
            tb = DbRules.Instance.GetSetInfo();
            smsdbDataSet.tb_SetRow row = null;
            if (selectRow.Length == 1)
            {
                row = tb[selectRow[0]];
                comSetModel.Text = row.SetType.ToString();
                comSetState.Text = row.State;
                comBaudRate.Text = row.BTL;

                comPortNum.Text = row.PortNum;
                txbSendSpeed.Text = row.Speed.ToString();
                txbRemark.Text = row.Remark;
                txbSuccCount.Text = row.SuccessCount.ToString();
                txbFailCount.Text = row.FailCount.ToString();
                txbReciveCount.Text = row.ReciveCount.ToString();
                txbSendMaxCount.Text = row.MaxCount.ToString();
                int maType = 0;
                int.TryParse(row.MaType, out maType);
                cbMaType.SelectedIndex = maType;
            }
        }

        void comPortNum_SelectedValueChanged(object sender, EventArgs e)
        {

        }

        private int GetComUseCount(smsdbDataSet.tb_SetRow inRow)
        {
            int count = 0;
            foreach (smsdbDataSet.tb_SetRow row in tb.Rows)
            {
                if (row.PortNum == this.comPortNum.Text && row.ID != inRow.ID)
                {
                    count++;
                }
            }
            return count;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (GetComUseCount(tb[selectRow[0]]) > 0)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("该Com口已存在，请选择其他端口!");
                return;
            }

            tb = DbRules.Instance.GetSetInfo();
            smsdbDataSet.tb_SetRow row = null;
            //if (selectRow.Length == 1)
            //{

            //    row = tb[selectRow[0]];
            //    row.State = comSetState.Text;
            //    tb.AcceptChanges();
            //    row.SetType = comSetModel.Text;
            //    row.BTL = comBaudRate.Text;

            //    row.Speed = int.Parse(txbSendSpeed.Text);
            //    row.Remark = txbRemark.Text;
            //    row.PortNum = comPortNum.Text;
            //    row.SuccessCount = int.Parse(txbSuccCount.Text);
            //    row.FailCount = int.Parse(txbFailCount.Text);
            //    row.ReciveCount = int.Parse(txbReciveCount.Text);
            //    row.MaxCount = int.Parse(txbSendMaxCount.Text);
            //}
            for (int i = 0; i < selectRow.Length; i++)
            {
                row = tb[selectRow[i]];
                //row.State = comSetState.Text;
                tb.AcceptChanges();
                row.SetType = comSetModel.Text;
                row.BTL = comBaudRate.Text;

                row.Speed = int.Parse(txbSendSpeed.Text);
                row.Remark = txbRemark.Text;
                row.PortNum = comPortNum.Text;
                row.SuccessCount = int.Parse(txbSuccCount.Text);
                row.FailCount = int.Parse(txbFailCount.Text);
                row.ReciveCount = int.Parse(txbReciveCount.Text);
                row.MaxCount = int.Parse(txbSendMaxCount.Text);
                row.MaType = cbMaType.SelectedIndex.ToString();
            }
            DbRules.Instance.UpdateSetInfo();
            tb.AcceptChanges();

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comSetState_SelectedValueChanged(object sender, EventArgs e)
        {
            if (comSetState.SelectedText == "启用")
            {
                if (ComHelper.Instance.CheckComPorts(comPortNum.Text, comBaudRate.Text))
                {

                }
                else
                {
                    comSetState.Text = "无法启用";
                }
            }

        }

        private void cbMaType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!CheckDog.IsSupp(cbMaType.SelectedIndex))
            {
                cbMaType.SelectedIndex = CheckDog.AvilibalInt();
            }

        }
    }
}